package com.jlnk.androidfmc.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import androidx.fragment.app.Fragment;

import com.jlnk.androidfmc.R;

public class FollowFragment extends Fragment {

    private ListView followList;
    private ArrayAdapter<FollowItem> adapter;

    // 模拟数据
    private final FollowItem[] followItems = new FollowItem[] {
            new FollowItem("叙利亚", "今天你想学习了嘛？", R.drawable.yhm),
            new FollowItem("麻虾", "今天我不想学习！！！", R.drawable.yhm),
            new FollowItem("萱", "我每天都不想学习！！！", R.drawable.yhm),
            new FollowItem("熊哥", "你说谁不是呢！！！", R.drawable.yhm)
    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_follow, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        // 初始化ListView
        followList = getView().findViewById(R.id.followList);

        // 创建适配器并设置给ListView
        adapter = new ArrayAdapter<FollowItem>(getActivity(),
                R.layout.item_follow, R.id.textViewName, followItems) {
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                if (convertView == null) {
                    convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_follow, parent, false);
                }

                ImageView imageViewAvatar = convertView.findViewById(R.id.imageViewAvatar);
                TextView textViewName = convertView.findViewById(R.id.textViewName);
                TextView textViewStatus = convertView.findViewById(R.id.textViewStatus);

                FollowItem item = getItem(position);

                imageViewAvatar.setImageResource(item.getAvatar());
                textViewName.setText(item.getName());
                textViewStatus.setText(item.getStatus());

                return convertView;
            }
        };

        followList.setAdapter(adapter);
    }

    // 定义一个简单的FollowItem模型
    public static class FollowItem {
        private String name;
        private String status;
        private int avatar;

        public FollowItem(String name, String status, int avatar) {
            this.name = name;
            this.status = status;
            this.avatar = avatar;
        }

        public String getName() {
            return name;
        }

        public String getStatus() {
            return status;
        }

        public int getAvatar() {
            return avatar;
        }
    }
}